package com.quanshu.exchange.ui.gui.message.chat.input;

import android.content.Context;
import android.os.Build;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.quanshu.exchange.R;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by jye on 2018/1/24.
 */

public class ChatRecorderView extends LinearLayout {

    @BindView(R.id.chat_voice_start_recorder_btn)
    ImageView recorderBtn;
    private View view;
    private DialogChatRecorder dialogChatRecorder;
    private Context context;

    public ChatRecorderView(Context context) {
        super(context);
        initView(context);
    }

    public ChatRecorderView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public ChatRecorderView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public ChatRecorderView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        initView(context);
    }

    private void initView(Context context) {
        this.context = context;
        view = LayoutInflater.from(context).inflate(R.layout.chat_voice_recorder_view, this);
        ButterKnife.bind(view);
    }

    public DialogChatRecorder.VoiceRecorderCallback getVoiceRecordCallback() {
        return callback;
    }

    private DialogChatRecorder.VoiceRecorderCallback callback;

    public void setVoiceRecordCallback(DialogChatRecorder.VoiceRecorderCallback callback) {
        this.callback = callback;
        initListener();
    }

    private void initListener() {
        recorderBtn.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent event) {

                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        if (dialogChatRecorder == null) {
                            dialogChatRecorder = new DialogChatRecorder(context);
                        }
                        if (!dialogChatRecorder.isShowing()) {
                            dialogChatRecorder.show();
                            dialogChatRecorder.onTouch(event, callback);
                        }
                        return true;
                    case MotionEvent.ACTION_MOVE:
                        dialogChatRecorder.onTouch(event, callback);
                        return true;
                    case MotionEvent.ACTION_UP:
                        dialogChatRecorder.onTouch(event, callback);
                        if (dialogChatRecorder != null) {
                            if (dialogChatRecorder.isShowing()) {
                                dialogChatRecorder.dismiss();
                            }
                        }
                        return true;
                    default:
                        return false;
                }
            }
        });
    }


}
